package Refractored;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;

public class ServerThread extends Thread {
	Socket clientSocket = null;
	long sleepTime = 0; // for use with Q3 only
	private static AtomicInteger numRequets = new AtomicInteger(); 

	ServerThread(Socket clientSocket){
		this.clientSocket = clientSocket;
	}

	public void run(){

		try {
			PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
			BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); 

			String inputLine;
			String outputLine;

			while ((inputLine = in.readLine()) != null) 
			{ 
				numRequets.incrementAndGet();
				System.out.println ("Serveur: " + inputLine);
				outputLine = inputLine.toUpperCase();
				out.println("#" + numRequets.intValue() + " - " + outputLine);
				if (inputLine.equals("Bye.")) 
					break; 
			} 

			out.close(); 
			in.close(); 
			clientSocket.close();
			
			System.out.println("Connection closed");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
